package com.st.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.st.entity.*;
import com.st.mapper.IAttractionMapper;
import com.st.mapper.IScenicDetailMapper;
import com.st.mapper.IScenicMapper;
import com.st.service.IScenicDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
public class ScenicDetailServiceImpl extends ServiceImpl<IScenicMapper, ScenicDetail> implements IScenicDetailService {
    @Autowired
    private IAttractionMapper iAttractionMapper;
    @Autowired
    private IScenicDetailMapper iScenicDetailMapper;
    //搜索+分页
    @Override
    public PageResult<ScenicDetail> search(PageDTO<ScenicDetail> pageDTO) {
        IPage<ScenicDetail> iPage = new Page<>(pageDTO.getPage(),pageDTO.getLimit());
        QueryWrapper<ScenicDetail> queryWrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(pageDTO.getData().getName())){
            queryWrapper.like("name",pageDTO.getData().getName());
        }
        if (!StringUtils.isEmpty(pageDTO.getData().getAttractionsId())){
            queryWrapper.eq("attractions_id",pageDTO.getData().getAttractionsId());
        }
        IPage<ScenicDetail> pageInfo = iScenicDetailMapper.selectPage(iPage, queryWrapper);
        if (pageInfo == null) {
            return new PageResult<>(false,"数据丢失");
        }
        PageResult<ScenicDetail> pageResult = new PageResult<>();
        pageResult.setSuccess(true);
        pageResult.setPages(pageInfo.getPages());
        pageResult.setTotal(pageInfo.getTotal());
        pageResult.setData(pageInfo.getRecords());
        return pageResult;
    }
    //查找所有景区
    @Override
    public Result scenicSearch() {
        QueryWrapper<Attractions> queryWrapper = new QueryWrapper<>();
        List<Attractions> attractionsList = iAttractionMapper.selectList(queryWrapper);
        return new Result(true,"景区列表",attractionsList);
    }
    //添加景区详情或编辑
    @Override
    public Result addScenicDetail(ScenicDetail scenicDetail) {
        System.out.println(scenicDetail);
//        Integer id = scenicDetail.getId();
//        String id = ;
        if (scenicDetail.getId()==null){
            int insert = iScenicDetailMapper.insert(scenicDetail);
            if (insert!=0){
                return new Result(true,"添加成功");
            }
        }else {
            int i = iScenicDetailMapper.updateById(scenicDetail);
            if (i!=0){
                return new Result(true,"修改成功");
            }
        }
        return new Result(false,"操作失败");
    }
    //
    @Override
    public Result edit(ScenicDetail scenicDetail) {
        return null;
    }
    //批量删除
    @Override
    public Result del(ScenicDetail scenicDetail) {
        int i = iScenicDetailMapper.deleteById(scenicDetail.getId());
        if (i!=0){
            return new Result(true,"删除成功");
        }
        return new Result(false,"删除失败");

    }

    @Override
    public Result delAll(List<Integer> ids) {
        int i = iScenicDetailMapper.deleteBatchIds(ids);
        if (i!=0){
            return new Result(true,"删除成功");
        }
        return new Result(false,"删除失败");
    }
}
